package com.example.zlw.dem14;



import jdk.management.resource.internal.inst.SocketOutputStreamRMHooks;

import java.awt.print.PrinterGraphics;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: Zlw
 * @description:
 * @author: 诸葛龙辰
 * @create: 2022-11-06 18:35
 **/
public class Test3 {
    public static void main(String[] args) {
        List<Lol> list = new ArrayList<>();
        list.add(new Lol("Uzi",22,"北京"));
        list.add(new Lol("李相赫",23,"韩国"));
        list.add(new Lol("茄子",33,"北京"));
        list.add(new Lol("馒头",28,"北京"));
        list.add(new Lol("大魔王龙哥",11,"鹿邑"));
        list.add(new Lol("赵龙威",22,"鹿邑彭于晏"));
        list.add(new Lol("包子",29,"东北"));
        list.add(new Lol("叶秋",30,"全职高手"));
        list.add(new Lol("苏沐月",27,"全职高手"));
        System.out.println("============Stream流的操作==========");
        System.out.println("--------------Stream中间操作-=------------");

        /**
         * filter筛选操作
         */
        System.out.println("=============filter==========");
        list.stream().filter(item->item.getAge()>25).forEach(System.out::println);
        /**
         * map映射操作
         */
        System.out.println("============map映射==========");
        list.stream().map(item-> item.getName()).forEach(System.out::println);
        /**
         * sort排序的操作
         */
        System.out.println("============sort的操作=============");
        list.stream().sorted((o1, o2) -> o1.getAge() - o2.getAge()).forEach(System.out::println);
         String arr[]={"11","12","13","14","15","16"};
         String arr1[]={"16","15","14","13","12","11"};
        Stream<String> arr2 = Stream.of(arr);
        Stream<String>  arr3 = Stream.of(arr1);
        /*concat*/
        System.out.println("------concat--------");
        /**
         * distinct去重
         */
        System.out.println("==========去重===========");
        /**
         * limit只要截取数据个数
         */
        System.out.println("-------limit-----");
/**
 * skip 去除数据中几个数据
 */
        System.out.println("========skip======");
        Stream.concat(arr2,arr3).skip(11).forEach(System.out::println);
        System.out.println("=====find======");
/**
 * findFirst获取序列的第一个数据
 *
 */
        System.out.println("========获取第一个数据===========");
        Optional<Lol> frist = list.stream().findFirst();
        System.out.println(frist);
        /**
         * findAny随机获取一条
         */
        System.out.println("==========findAny========");
        Optional<Lol> any = list.stream().findAny();
        System.out.println(any);
        System.out.println("============match===========");
        /**
         * allMatch所以条件需满足才能实现
         */
        System.out.println("============allMatch=========");
        boolean b1 = list.stream().anyMatch(item->item.getAge()>11);
        System.out.println(b1);
        System.out.println("===========Count聚合操作==================");
        /**
         * max最大值
         */
        System.out.println("========max==============");
        Optional<Lol> max = list.stream().max((o1, o2) -> o1.getAge()- o2.getAge());
        System.out.println(max);
        /**
         * min最小值
         */
        System.out.println("========min======");
        System.out.println("========max最大值===============");
        Optional<Lol> min = list.stream().min((o1, o2) -> o1.getAge() - o2.getAge() );
        System.out.println(min);
        /**
         * count总数量
         */
        System.out.println("==========++++++++=============");
        long count = list.stream().count();
        System.out.println(count);
        Integer [] arr0={2,3,4,5,6,2};
        System.out.println("=============reduce规约操作可以进行加减乘除和最值===========");
        Optional<Integer> red = Arrays.stream(arr0).reduce((o1,o2)-> o1-o2);
        Optional<Integer> red1 = Arrays.stream(arr0).reduce((o1,o2)-> o1-o2);
        Optional<Integer> red2 = Arrays.stream(arr0).reduce((o1,o2)-> o1-o2);
        System.out.println(red);
        System.out.println("=============collect搜集操作==========");
        /**
         * 获取新的序列集合
         */
        List<Lol> collect = list.stream().collect(Collectors.toList());
        /**
         * 获取新的map集合
         */
        Map<String,Lol> collect1 = list.stream().collect(Collectors.toMap(item-> item.getName(),item ->item));
        /**
         * 分组
         */
        Map<String, List<Lol>> collect2 = list.stream().collect(Collectors.groupingBy(item -> item.getName()));
        System.out.println(collect2);
        /**
         * 最大值
         */
        Optional<Lol> collect3 = list.stream().collect(Collectors.maxBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println(collect3);
    }
}
class Lol{
    private String name;
    private  int age;
    private  String address;


    public Lol() {
    }

    public Lol(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Lol{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}